<template>
  <div>
    <a-card :bordered="false">
      <f-search-form  @searchHandler="searchHandler">
        <f-search-form-item label="专业包代码" name="majorCode" slot="advanced" />
        <f-search-form-item label="专业包名称" name="majorName"/>
        <f-search-form-item label="工程包代码" name="packageNo" slot="advanced" />
        <f-search-form-item label="工程包名称" name="packageName"/>
        <f-search-form-item label="材料名称" name="materialName"/>
        <!-- <f-search-form-item label="规格型号" name="materiaSpecs"/> -->
        <f-search-form-item label="项目名称" name="materiaSpecs"/>
        <f-search-form-item label="材料WBS" name="materialWbsCode" slot="advanced" />
        <f-search-form-item label="材料代码" name="materialCode" slot="advanced" />
        <f-search-form-item label="层级号" name="levelNo" slot="advanced" />
        <f-search-form-item label="科目名称" name="subjectName" slot="advanced" />
        <f-search-form-item label="区域" name="materialArea" slot="advanced" />
        <f-search-form-item label="系统" name="materialUse" slot="advanced" />
      </f-search-form>
      <f-table ref="table" rowKey="materialPlanId" :scroll="{x:1980,y:columns.length*35}" :columns="columns" :exportBtn="false" :multiple="false" :operator="operator" :params="queryParam" :api="api.bmep" handler="materialPlanPageListHandler">
        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleAdd(record)">添加</a>
          </template>
        </span>
      </f-table>
      <a-divider orientation="left">添加</a-divider>
      <a-table bordered :scroll="{x:1980}" :data-source="dataSource" :columns="columns2">
        <!-- 编辑 -->
        <template v-for="col in editSlots" :slot="col" slot-scope="text, record, index">
          <div :key="col">
            <a-input
              v-if="record.materialPlanId===editingKey"
              style="margin: -5px 0"
              :value="text"
              @change="e => handleChange(e.target.value, record.materialPlanId, col)"
            />
            <template v-else>
              {{ text }}
            </template>
          </div>
        </template>
        <template slot="action" slot-scope="text, record, index">
          <a-popconfirm
            v-if="dataSource.length"
            title="Sure to delete?"
            @confirm="() => onDelete(record.materialPlanId)"
          >
            <a href="javascript:;">删除</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
          <div class="editable-row-operations">
              <span v-if="record.materialPlanId===editingKey">
                <a @click="() => save(record.materialPlanId)">保存</a>
                <a-popconfirm title="Sure to cancel?" @confirm="() => cancel(record.materialPlanId)">
                  <a  @click="() => cancel(record.materialPlanId)">取消</a>
                </a-popconfirm>
              </span>
            <span v-else>
                <a :disabled="editingKey !== ''" @click="() => edit(record.materialPlanId)">编辑</a>
              </span>
          </div>
        </template>
      </a-table>
    <upload-modal ref="uploadModal" :action="api.bmep.import100AddData" :otherParams="queryParam"
                  @change="uploadEnd"></upload-modal>
    </a-card>
    <bmp-drawer ref="bmpDrawer"></bmp-drawer>
  </div>
</template>
<script>
  import bmpDrawer from './bmp_drawer.vue'
  import api from './js/api'
  export default {
    name: 'TableList',
    components: {
      bmpDrawer
    },
    props: {
      versionId: {
        type: String,
        default: ''
      },
      projectId:{
        type: String,
        default: ''
      }
    },
    data() {
      let me = this
      //操作按钮
      let operator = []
      operator.push({
        name: '添加',
        icon: '',
        that: this,
        handler: function(table, that) {
          let  projectIds = that.queryParam.projectId
          that.$refs.bmpDrawer.showDrawer(projectIds,2)
        }
      })
      operator.push({
        name: '提交',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.copyAndUpdate(that.cacheData)
        }
      })
      operator.push({
        name: '下载清单',
        icon: '',
        that: this,
        handler: function(table, that) {
          let columns = table.getExportColumns(that.columns)
          columns.push({ name: '材料id', key: 'materialPlanId' })
          columns.push({ name: '是否新增', key: 'isAdd' })
          columns.push({ name: '数据类型', key: 'dataType' })
          that.queryParam['columns'] = JSON.stringify(columns)
          that.queryParam.packageName = table.parameter.packageName
          that.queryParam.majorCode = table.parameter.majorCode
          that.queryParam.majorName = table.parameter.majorName
          that.queryParam.packageNo = table.parameter.packageNo
          that.queryParam.materiaSpecs = table.parameter.materiaSpecs
          that.queryParam.materialWbsCode = table.parameter.materialWbsCode
          that.queryParam.materialCode = table.parameter.materialCode
          that.queryParam.levelNo = table.parameter.levelNo
          that.queryParam.subjectName = table.parameter.subjectName
          that.queryParam.materialArea = table.parameter.materialArea
          that.queryParam.materialUse = table.parameter.materialUse
          that.download100plan()
        }
      })
      operator.push({
        name: '导入数据',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.$refs.uploadModal.open()
        }
      })
      operator.push({
        name: '提交oa合并审批',
        icon: '',
        that: this,
        handler: function(table, that) {
          that.http.get(that, {
            url: that.api.project.projectStatus,
            data: {
              projectId: that.queryParam.projectId
            },
            success: function(me, data) {
              let projectStatus = data
              if (projectStatus == 1) {
                me.$message.error('项目已锁定不支持操作')
              } else {
                that.margeplan100()
              }
            }
          })
        }
      })
      return {
        api:  Object.assign(api, this.api),
        //列表初始化查询参数
        queryParam: {
          projectId: me.projectId,
          versionId: me.versionId
        },
        // 表头
        columns: [
          {
            title: '序号',
            width: 60,
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '专业代码',
            width: 100,
            align: 'center',
            dataIndex: 'majorCode'
          },
          {
            title: '专业名称',
            width: 100,
            align: 'center',
            dataIndex: 'majorName'
          },
          {
            title: '工程包名称',
            width: 100,
            align: 'center',
            dataIndex: 'packageName'
          },
          {
            title: '工程包代码',
            width: 100,
            align: 'center',
            dataIndex: 'packageNo'
          },
          {
            title: '物料代码',
            width: 100,
            align: 'center',
            dataIndex: 'materialCode'
          },
          {
            title: '物料wbs代码',
            width: 120,
            align: 'center',
            dataIndex: 'materialWbsCode'
          },
          {
            title: '设备、材料名称',
            width: 200,
            align: 'center',
            dataIndex: 'materialName'
          },
          // {
          //   title: '型号/规格',
          //   width: 200,
          //   align: 'center',
          //   dataIndex: 'materiaSpecs'
          // },
          // {
          //   title: '项目名称',
          //   width: 200,
          //   align: 'center',
          //   dataIndex: 'materiaSpecs'
          // },
          // {
          //   title: '技术参数或技术说明',
          //   width: 200,
          //   align: 'center',
          //   dataIndex: 'technicalData'
          // },
          {
            title: '对外合同清单描述',
            width: 200,
            align: 'center',
            dataIndex: 'contractDesc',
            scopedSlots: { customRender: 'contractDesc' }
          },
          {
            title: '区域',
            width: 60,
            align: 'center',
            dataIndex: 'materialArea',
            scopedSlots: { customRender: 'materialArea' }
          },
          // {
          //   title: '系统',
          //   width: '100',
          //   align: 'center',
          //   dataIndex: 'materialUse',
          //   scopedSlots: { customRender: 'materialUse' }
          // },

          {
            title: '层级号',
            width: 100,
            align: 'center',
            dataIndex: 'levelNo',
            scopedSlots: { customRender: 'levelNo' }
          },
          {
            title: '计量单位',
            width: 100,
            align: 'center',
            dataIndex: 'measurementUnit'
          },
          {
            title: '换算单位',
            width: 100,
            align: 'center',
            dataIndex: 'conversionUnit'
          },

          // {
          //   title: '100工程量计量单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'unitBillQuantity100'
          // },
          // {
          //   title: '100工程量换算单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'convertBillQuantity100'
          // },
          // {
          //   title: '200工程量计量单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'unitBillQuantity200'
          // },
          // {
          //   title: '200工程量换算单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'convertBillQuantity200'
          // },
          {
            title: '100预算成本工程量',
            width: 150,
            align: 'center',
            dataIndex: 'costQuantity100'
          },
          {
            title: '200预算成本工程量',
            width: 150,
            align: 'center',
            dataIndex: 'costQuantity200'
          },
          {
            title: '损耗',
            width: 60,
            align: 'center',
            dataIndex: 'loss'
          },
          {
            title: '100采购计划量',
            width: 120,
            align: 'center',
            dataIndex: 'planPurchaseCount'
          },
          {
            title: '备注',
            width: 120,
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '指令号',
            width: 100,
            align: 'center',
            dataIndex: 'instructNumber'
          },
          {
            title: '审批状态',
            width: 100,
            align: 'center',
            dataIndex: 'applayStatus',
            customRender: function (text) {
               return me.columnFormatter(me.api.bmep.applayStatus, text)
             }
          },
          {
            title: '操作',
            fixed: 'right',
            width: 80,
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        operator: operator,
        columns2: [
          {
            title: '序号',
            width: 60,
            align: 'center',
            customRender: (text, record, index) => index + 1
          },
          {
            title: '专业代码',
            width: 100,
            align: 'center',
            dataIndex: 'majorCode'
          },
          {
            title: '专业名称',
            width: 100,
            align: 'center',
            dataIndex: 'majorName'
          },
          {
            title: '工程包名称',
            width: 100,
            align: 'center',
            dataIndex: 'packageName'
          },
          {
            title: '工程包代码',
            width: 100,
            align: 'center',
            dataIndex: 'packageNo'
          },
          {
            title: '物料代码',
            width: 100,
            align: 'center',
            dataIndex: 'materialCode'
          },
          {
            title: '物料wbs代码',
            width: 100,
            align: 'center',
            dataIndex: 'materialWbsCode'
          },
          {
            title: '设备、材料名称',
            width: 200,
            align: 'center',
            dataIndex: 'materialName',
            scopedSlots: { customRender: 'materialName' }
          },
          // {
          //   title: '型号/规格',
          //   width: 200,
          //   align: 'center',
          //   dataIndex: 'materiaSpecs',
          //   scopedSlots: { customRender: 'materiaSpecs' }
          // },
          {
            title: '型号/规格',
            width: 200,
            align: 'center',
            dataIndex: 'materiaSpecs',
            scopedSlots: { customRender: 'materiaSpecs' }
          },
          // {
          //   title: '技术参数或技术说明',
          //   width: 200,
          //   align: 'center',
          //   dataIndex: 'technicalData',
          //   scopedSlots: { customRender: 'technicalData' }
          // },
          {
            title: '对外合同清单描述',
            width: 200,
            align: 'center',
            dataIndex: 'contractDesc',
            scopedSlots: { customRender: 'contractDesc' }
          },
          {
            title: '区域',
            width: 100,
            align: 'center',
            dataIndex: 'materialArea',
            scopedSlots: { customRender: 'materialArea' }
          },
          // {
          //   title: '系统',
          //   width: '100',
          //   align: 'center',
          //   dataIndex: 'materialUse',
          //   scopedSlots: { customRender: 'materialUse' }
          // },

          {
            title: '层级号',
            width: 100,
            align: 'center',
            dataIndex: 'levelNo',
            scopedSlots: { customRender: 'levelNo' }
          },
          {
            title: '计量单位',
            width: 100,
            align: 'center',
            dataIndex: 'measurementUnit',
            scopedSlots: { customRender: 'measurementUnit' }
          },
          {
            title: '换算单位',
            width: 100,
            align: 'center',
            dataIndex: 'conversionUnit',
            scopedSlots: { customRender: 'conversionUnit' }
          },
          // {
          //   title: '100工程量计量单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'unitBillQuantity100',
          //   scopedSlots: { customRender: 'unitBillQuantity100' }
          // },
          // {
          //   title: '100工程量换算单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'convertBillQuantity100',
          //   scopedSlots: { customRender: 'convertBillQuantity100' }
          // },
          // {
          //   title: '200工程量计量单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'unitBillQuantity200',
          //   scopedSlots: { customRender: 'unitBillQuantity200' }
          // },
          // {
          //   title: '200工程量换算单位合计',
          //   width: '220',
          //   align: 'center',
          //   dataIndex: 'convertBillQuantity200',
          //   scopedSlots: { customRender: 'convertBillQuantity200' }
          // },
          {
            title: '100预算成本工程量',
            width: 150,
            align: 'center',
            dataIndex: 'costQuantity100',
            scopedSlots: { customRender: 'costQuantity100' }
          },
          {
            title: '200预算成本工程量',
            width: 150,
            align: 'center',
            dataIndex: 'costQuantity200',
            scopedSlots: { customRender: 'costQuantity200' }
          },
          {
            title: '损耗',
            width: 100,
            align: 'center',
            dataIndex: 'loss'
          },
          // {
          //   title: '本次采购量',
          //   width: '30',
          //   align: 'center',
          //   dataIndex: 'purchaseCount',
          //   scopedSlots: { customRender: 'purchaseCount' }
          // // // },
          // {
          //   title: '在途',
          //   width: '30',
          //   align: 'center',
          //   dataIndex: 'ztPurchaseCount'
          // },
          // {
          //   title: '到货',
          //   width: '50',
          //   align: 'center',
          //   dataIndex: 'dhPurchaseCount'
          // },
          // {
          //   title: '计划到货时间',
          //   width: '100',
          //   align: 'center',
          //   dataIndex: 'receivedDate'
          // },
          {
            title: '100采购计划量',
            width: 150,
            align: 'center',
            dataIndex: 'planPurchaseCount'
          },
          {
            title: '备注',
            width: 100,
            align: 'center',
            dataIndex: 'remark'
          },
          {
            title: '指令号',
            width: 80,
            align: 'center',
            dataIndex: 'instructNumber',
            scopedSlots: { customRender: 'instructNumber' }
          },
          {
            title: '审批状态',
            width: 100,
            align: 'center',
            dataIndex: 'applayStatus',
            customRender: function (text) {
              return me.columnFormatter(me.api.bmep.henqing , text)
            }
          },
          {
            title: '操作',
            fixed: 'right',
            width: 100,
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],

        dataSource: [],
        editSlots: ['costQuantity100', 'instructNumber'],
        editingKey: '',
        count: 1,
        cacheData: [],
        otherParams:{}
      }
    },
    created() {
      this.otherParams = this.queryParam
      this.otherParams.dataType = '2'
    },
    methods: {
      onCellChange(key, dataIndex, value) {
        const dataSource = [...this.dataSource]
        const target = dataSource.find(item => item.key === key)
        if (target) {
          target[dataIndex] = value
          this.dataSource = dataSource
        }
      },
      onDelete(key) {
        const dataSource = [...this.dataSource]
        this.dataSource = dataSource.filter(item => item.materialPlanId !== key)
        this.editingKey = ''
      },
      handleAdd(record) {
        if (this.editingKey){
          this.$message.error('您有未保存的数据！')
          return
        }
        if (record.applayStatus === '1') {
          this.$message.error('您操作的数据正在审批中')
          return
        }
        //复制一条
        let newData = JSON.stringify(record)
        newData = JSON.parse(newData)
        //设置新添加的变更数据 审批状态为0 --
        newData['applayStatus'] = 0
        this.dataSource = [...this.dataSource, newData]
        this.count += 1
        this.cacheData = this.dataSource.map(item => ({...item}))
      },
      handleChange(value, key, column) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => key === item.materialPlanId)[0]
        if (target) {
          target[column] = value
          this.dataSource = newData
        }
      },
      edit(key) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => key === item.key)[0]
        this.editingKey = key
        if (target) {
          target.editable = true
          this.dataSource = newData
        }
      },
      save(key) {
        const newData = [...this.dataSource]
        const newCacheData = [...this.cacheData]
        const target = newData.filter(item => key === item.materialPlanId)[0]
        const targetCache = newCacheData.filter(item => key === item.materialPlanId)[0]
        if (target && targetCache) {
          if (!target.instructNumber) {
            this.$message.error('指令号不能为空')
            return
          }
          delete target.editable
          this.dataSource = newData
          Object.assign(targetCache, target)
          this.cacheData = newCacheData
        }
        this.editingKey = ''
      },
      cancel(key) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => key === item.materialPlanId)[0]
        this.editingKey = ''
        if (target) {
          i
          Object.assign(target, this.cacheData.flter(item => key === item.materialPlanId)[0])
          delete target.editable
          this.data = newData
        }
      },
      copyAndUpdate(cacheData) {
        let that = this
        let copyData = JSON.stringify(cacheData)
        console.log(copyData)
        that.http.post(that, {
          url: that.api.bmep.copyAndUpdate,
          data: {
            copyDataStr: copyData,
            planType: 2,
            projectId: this.queryParam.projectId
          },
          success(me, data) {
            that.cacheData = []
            that.dataSource = []
            that.$refs.table.refresh(false, that.queryParam)
          }
        })
      },
      download100plan() {
        let that = this
        that.http.exportExcel(that, {
          url: that.api.bmep.export100planTemplate,
          data: that.queryParam
        })
      },
      uploadEnd(file, fileList, event) {
        this.$refs.uploadModal.close()
        let errMsg = ""
        if(fileList.response.code == "100") {
          errMsg = '导入成功'
          this.$message.success(errMsg)
        } else {
          errMsg = fileList.response.message
          this.$message.error(errMsg)
        }
        this.$refs.table.refresh(false, this.queryParam)
      },
      margeplan100() {
        let that = this
        that.http.post(that, {
          url: that.api.bmep.margeplan100,
          data: {
            projectId: that.otherParams.projectId,
            dataType: that.otherParams.dataType
          },
          success(me, data) {
            me.$message.success(data)
          }
        })
      }
    }
  }
</script>
